#!/bin/bash
#
#===================================================================
# find-vi wrapper
#===================================================================
# arguments process
# if given a valid file, try to open it
# if given a number, try to open file of ffa[number]
# if given -d with valid directry, try to find all .$EXT files inisde
# if given -d with invalid directory, try to list all similar directories
# if given .*, use it as filter for target file, default is .scala
# in any case, if valid files founded, you can select one to open 
#
# if given -h, modify the list count limitation ($ffh)
#===================================================================

#declare -A apps
#apps=([.pdf]="evince" [.v]="vim" [.scala]="vim")

EXT=".scala"
EXE="vim"

argc=0
args=("$@")

# head limit
ffh=20

fff=""
ffn=""
ffg=""
ffa=()

# file find option
ff_d="*"
ff_r="."
ff_t="f"

[[ ! -z "${FVI_EXT}" ]] && EXT=${FVI_EXT}

ff_c="*$EXT"

skip=0
for argv in $*
do
  let "argc+=1"
  [[ $skip == 1 && $argv != "-"* ]] && skip=0 && continue
  [[ -f "$argv" ]] && fff=$argv && continue
  [[ $argv == "-h" ]] && ffh=${args[$argc]} && skip=1 && continue
  [[ $argv == "-s" ]] && ffg=${args[$argc]} && skip=1 && continue
  [[ $argv == "-c" ]] && ffg=${args[$argc]} && skip=1 && continue
  [[ $argv == "-e" ]] && EXE=${args[$argc]} && skip=1 && continue
  [[ $argv =~ ^[0-9]+$ ]] && ffn=$argv && continue
  [[ ${argv} =~ ^\.[a-zA-Z]+$ ]] && EXT=$argv && ff_c="*$argv" && continue
  
  if [[ $argv == "-v" ]]; then
    [[ -f "/tmp/.fvi" ]] && cat /tmp/.fvi
    exit 0
  fi

  if [[ $argv == "-d" ]]; then
   if [[ -d "${args[$argc]}" ]]; then
     ff_r=${args[$argc]}
     ff_d="*"
   else
     ff_d="*${args[$argc]}*"
     ff_c="*"
     ff_t="d"
   fi
   skip=1 && continue
  fi

  gff=$(which $argv)
  if [[ -r "$gff" ]]; then
    #fgff=$(file $gff)
    #fgff=$(cut -d":" -f 1 <<< $(file $gf))
    fff=$gff
  fi
done

#[[ -z ${apps[$EXT]} ]] && EXE="vim" || EXE=${apps[$EXT]}

if [[ "$fff" != "" && -f "$fff" ]]; then
  $EXE "$fff"
else
  fn=0
  ff=""
  fs=$ffh
  #files=$(find $ff_r -type $ff_t -ipath "$ff_d" -iname "$ff_c" | xargs -i echo {})
  #files=$(find $ff_r -type $ff_t -ipath "$ff_d" -iname "$ff_c" -exec echo {} \;)

  [[ "$ffg" != "" ]] && echo "Search file contains \"$ffg\""
  [[ "$ffn" == "" ]] && rm -f /tmp/.fvi.bak

  #for ff in $files
  if [[ $ffn =~ ^[0-9]+$ ]]; then
    if [[ ! -f "/tmp/.fvi" ]]; then
       echo "No cache list found!"
       echo "Please run cmd with other options firstly!"
    else
       line=""
       line=$(sed -n "/^$ffn/p" /tmp/.fvi)
       [[ "$line" != "" ]] && ff=$(echo ${line#*:} | xargs) && echo $ff
    fi
  elif [[ "$ffg" == "" ]]; then
    #while IFS= read -r -d '' -u 9
    while IFS= read -r line
    do
      if [[ "$line" != "." && "$ffn" == "" ]]; then
        echo $fn : $line
	rline=$(echo "$line" | sed -e "s/^\.\///")
        echo $fn : $(echo `pwd`)/"$rline" >> /tmp/.fvi.bak
      fi
      [[ $ffn =~ ^[0-9]+$ ]] && printf "."
      ffa+=("$line")
      [[ $ffn =~ ^[0-9]+$ && "$fn" == "$ffn" ]] && echo "" && echo "$line" && break
      let fn+=1
      if [[ "$fn" == "$fs" && "$ffn" == "" ]]; then
        let fs+=$ffh
        read -p "Select file number or <Enter> to continue: " yon
        [[ $yon =~ ^[0-9]+$ ]] && ffn=$yon && break
      fi
    done < <(find $ff_r -type $ff_t -ipath "$ff_d" -iname "$ff_c" | xargs -i echo {})
    #done 9< <(find $ff_r -type $ff_t -ipath "$ff_d" -iname "$ff_c" -print0)
  else 
    while read -r line ; do
      [[ $ffn =~ ^[0-9]+$ ]] && printf "."
      [[ "$line" != $ff_c ]] && continue
      echo $fn : $line
      echo $fn : $(pwd)/"$line" >> /tmp/.fvi.bak
      ffa+=("$line")
      [[ $ffn =~ ^[0-9]+$ && "$fn" == "$ffn" ]] && echo "" && echo "$line" && break
      let fn+=1
      if [[ "$fn" == "$fs" && "$ffn" == "" ]]; then
        let fs+=$ffh
        read -p "Select file number or <Enter> to continue: " yon
        [[ $yon =~ ^[0-9]+$ ]] && ffn=$yon && break
      fi
    done < <(grep -rl "$ffg" "$ff_r")
  fi

  # update list cache
  [[ -f "/tmp/.fvi.bak" ]] && cat /tmp/.fvi.bak > /tmp/.fvi

  if [[ $ffn =~ ^[0-9]+$ ]]; then
    [[ -r "$ff" ]] && $EXE "$ff" || $EXE "${ffa[$ffn]}"
  elif [[ "$fn" == "1" ]]; then
    $EXE "${ffa[0]}"
  elif [[ "$fn" != "0" && $ff_c != "" ]]; then
    read -p "Select target file number: " yon
    [[ $yon =~ ^[0-9]+$ ]] && $EXE "${ffa[$yon]}"
  elif [[ $ff_c != "" ]]; then
    echo -e "No file found of type \"$ff_c\""
  fi
fi
